package com.atguigu.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.atguigu.commonutils.Result;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.dto.course.CourseInfoDTO;
import com.atguigu.eduservice.entity.dto.course.CourseListQueryDTO;
import com.atguigu.eduservice.entity.vo.course.CourseInfoVO;
import com.atguigu.eduservice.entity.vo.course.CourseListQueryVO;
import com.atguigu.eduservice.entity.vo.course.CoursePublishVo;
import com.atguigu.eduservice.service.EduCourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2024-06-23
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
@Slf4j
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    //课程列表基本实现
    @PostMapping(value = "pageCourseCondition/{current}/{limit}")
    public Result pageCourseCondition(@PathVariable long current, @PathVariable long limit,
                                       @RequestBody(required = false) CourseListQueryDTO courseListQueryDTO) {

        Page<EduCourse> eduCourse = new Page<>(current,limit);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        String title = courseListQueryDTO.getTitle();
        String status = courseListQueryDTO.getStatus();
        if(title !=null ){
            wrapper.like("title",title);
        }
        if (status != null){
            wrapper.eq("status",status);//等于
        }
        eduCourseService.page(eduCourse,wrapper);

        long total = eduCourse.getTotal();
        List<EduCourse> records = eduCourse.getRecords();
        ArrayList<CourseListQueryVO> courseListQueryVO = new ArrayList<>();
        for(EduCourse eduCourse1:records){
            CourseListQueryVO courseListQueryVO1 = new CourseListQueryVO();
            BeanUtils.copyProperties(eduCourse1,courseListQueryVO1);
            courseListQueryVO.add(courseListQueryVO1);
        }
        return Result.ok().data("total",total).data("items",courseListQueryVO);

    }

    @PostMapping("addCourseInfo")
    public Result addCourseInfo(@RequestBody CourseInfoDTO courseInfoDTO) {

        //返回id 为后面编写大纲做铺垫
        String id = eduCourseService.saveCourseInfo(courseInfoDTO);
        return Result.ok().data("courseId",id);
    }

    @GetMapping("getCourseInfo/{courseId}")
    public Result getCourseInfo(@PathVariable String courseId) {
        CourseInfoVO courseInfoVO =eduCourseService.getCourseInfo(courseId);
        log.info("根据课程id查询课程信息:{}",courseInfoVO);
        return Result.ok().data("courseInfoVO",courseInfoVO);
    }
    @PostMapping("updateCourseInfo")
    public Result updateCourseInfo(@RequestBody CourseInfoDTO courseInfoDTO) {
        eduCourseService.updateCourseInfo(courseInfoDTO);
        return Result.ok();
    }

    //根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{courseId}")
    public Result getPublishCourseInfo(@PathVariable String courseId) {
        CoursePublishVo coursePublishVo = eduCourseService.getCoursePublishVo(courseId);
        log.info("根据课程id查询课程确认信息:{}",coursePublishVo);
        return Result.ok().data("coursePublishVo",coursePublishVo);
    }


    //删除课程信息
    @DeleteMapping("deleteCourse/{courseId}")
    public Result deleteCourse(@PathVariable String courseId) {
        eduCourseService.removeCourse(courseId);
        return Result.ok();
    }







}

